#!/usr/bin/env python

"""

This is a Python adaptation of the FinancialHistory class used as an example
in the classic book Smalltalk-80: The Language, by Adele Goldberg and David
Robson (ISBN 0-201-13688-0).

To use it, we first create an instance of FinancialHistory::

    >>> fh = FinancialHistory()
    >>> fh.cashOnHand
    0
    
Let's put some money into it...

::

    >>> fh.receiveFrom('mother', 500)
    >>> fh.receiveFrom('father', 10)
    >>> fh.cashOnHand
    510

and then spend part of it, get some more, wast another chunk::

    >>> fh.spendFor('poker', 300)
    >>> fh.cashOnHand
    210
    >>> fh.receiveFrom('mother', 200)
    >>> fh.spendFor('poker', 150)
    >>> fh.cashOnHand
    260
    
How did we get and spent our money?

    >>> fh.totalReceivedFrom('mother')
    700
    >>> fh.totalReceivedFrom('father')
    10
    >>> fh.totalReceivedFrom('job')
    0
    >>> fh.totalSpentFor('poker')
    450
    
"""

class FinancialHistory(object):
    
    def __init__(self, initialBalance=0):
        self.cashOnHand = initialBalance
        self.incomes = {}
        self.expenditures = {}
        
    def receiveFrom(self, source, amount):
        self.incomes[source] = self.totalReceivedFrom(source) + amount
        self.cashOnHand += amount
        
    def spendFor(self, reason, amount):
        self.expenditures[reason] = self.totalSpentFor(reason) + amount
        self.cashOnHand -= amount
        
    def totalReceivedFrom(self, source):
        return self.incomes.get(source, 0)
    
    def totalSpentFor(self, reason):
        return self.expenditures.get(reason, 0)
        
if __name__ == '__main__':
    import doctest
    doctest.testmod()


    
